package com.lyouu.dataminer.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.lyouu.dataminer.factory.DaoFactory;
import com.lyouu.dataminer.factory.MysqlDbFactory;

public class DbLyouuParams {
	private static DbLyouuParams singleton;
//	public HashMap<String, Integer> areaAttid;
	public int sid;
	public int albumId;
	public int picId;
	
	public static DbLyouuParams getParams() {
		if (singleton == null) {
			synchronized (DbLyouuParams.class) {
				try {
					singleton = new DbLyouuParams();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return singleton;
	}

	private DbLyouuParams() throws SQLException {
		Connection conn = MysqlDbFactory.getConnection(DbConstants.DB_LYOUU);
		int max;
		// query areaAttid
//		areaAttid = new HashMap<String, Integer>();
		String sql = "SELECT attid, name FROM md_area";
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
//		while (rs.next()) {
//			areaAttid.put(rs.getString(2), rs.getInt(1));
//		}
		// query max sid
		max=0;
		sql = "SELECT sid FROM md_subject";
		rs = stmt.executeQuery(sql);
		while (rs.next()) {
			if (rs.getInt(1) > max)
				max = rs.getInt(1);
		}
		sid = max;
		//query max albumId
		sql = "SELECT albumid FROM md_album";
		rs = stmt.executeQuery(sql);
		while (rs.next()) {
			if (rs.getInt(1) > max)
				max = rs.getInt(1);
		}
		albumId = max;
		
		sql = "SELECT picid FROM md_pictures";
		rs = stmt.executeQuery(sql);
		while (rs.next()) {
			if (rs.getInt(1) > max)
				max = rs.getInt(1);
		}
		picId = max;
		
		conn.close();
	}
	
	public int getNextSid(){
		return ++sid;
	}
	
	public int getNextAlbumId(){
		return ++albumId;
	}
	
	public int getNextPicId(){
		return ++picId;
	}

	public int getMinSidByOwner(String owner, int catid) throws SQLException{
		return DaoFactory.getMdSubjectDao().getMinSidByOwner(owner, catid);
	}
	
	public int getMaxSidByOwner(String owner,int catid) throws SQLException{
		return DaoFactory.getMdSubjectDao().getMaxSidByOwner(owner, catid);
	}
}
